﻿$(function () {
    $.ajaxSetup({ cache: false });

    document.confirmDialog = $('#confirmDialog');

    $(document.body).on('click', 'button[data-editor]', function () {
        var me = $(this);
        document.modelId = me.data('id');
        document.refId = me.data('refid');
        switch (me.data('editor')) {
            case 'create':
            case 'edit':
                $('#modalLabel').text(document.modalLabel1);
                getForm('EditSpecificationGroup', document.modelId, document.currentCulture, $('#specification-groups').data('refid'));
                break;
            case 'remove':
                document.confirmDialog.find('h4').text(document.confirmLabel1);
                document.confirmDialog.find('.modal-body').text(document.confirmContent1);
                document.confirmDialog.data('param', 1);
                document.confirmDialog.modal('show');
                break;
            case 'create-specification':
            case 'edit-specification':
                $('#modalLabel').text(document.modalLabel2);
                getForm('EditSpecification', document.modelId, document.currentCulture, document.refId);
                break;
            case 'remove-specification':
                document.confirmDialog.find('h4').text(document.confirmLabel2);
                document.confirmDialog.find('.modal-body').text(document.confirmContent2);
                document.confirmDialog.data('param', 2);
                document.confirmDialog.modal('show');
                break;
            case 'edit-values':
                $('#modalLabel').text(document.modalLabel3);
                getForm('SpecificationValues', document.modelId, document.currentCulture);
                break;
        }
    });

    $(document.body).on('click', '.langarea button', function () {
        var culture = $(this).data('culture');
        var form = $('#editorModal form');
        getForm(form.data('action'), form.data('modelid'), culture, form.data('refid'));
    });

    $('#confirmDialog button[data-yes]').click(function () {
        if (document.confirmDialog.data('param') === 1) {
            removeModel('RemoveSpecificationGroup', document.modelId);
        } else {
            removeModel('RemoveSpecification', document.modelId);
        }
    });

    $('#editorModal').on('show.bs.modal', function () {
        $('#editorModalLoading').show();
    });

    $('#editorModal').on('shown.bs.modal', function () {
        $('#formctnr form input[autofocus]').focus();
        updateSorting();
    });

    $(document.body).on('click', '#specification-values form .btn-danger', function () {
        var id = $(this).data('id');
        $('#sv-' + id).addClass('bg-danger');
        $.post('RemoveSpecificationValue', { id: id }, function (result) {
            if (result.status === 200) {
                $('#sv-' + id).remove();
                updateSorting();
            } else {
                alert(result.message);
            }
        });
    });

    $(document.body).on('submit', '#specification-values form', function () {
        var me = $(this);
        me.find('.glyphicon-floppy-disk').removeClass('glyphicon-floppy-disk').addClass('glyphicon-refresh').addClass('glyphicon-refresh-animate');
        me.find(":input").prop('readonly', true);
        me.find("button").prop('disabled', true);
        me.parent().addClass('disabled');
    });

    $(document.body).on('click', '#specification-values form .btn-success', function () {
        var sv0 = $('#sv-0');
        if (sv0.length === 0) {
            var me = $(this);
            $.get('EditSpecificationValue', { id: 0, specificationid: me.data('specificationid') }, function (result) {
                $('#specification-values').append(result);
            });
        } else {
            sv0.find('input[autofocus]').focus();
        }
    });
});

function updateSorting() {
    $('.sortable').sortable({
        forcePlaceholderSize: true,
        handle: '.glyphicon-move',
        placeholderClass: 'list-group-item-warning'
    }).bind('sortupdate', function (e, ui) {
        console.log(ui.item);
    });
}

function getForm(action, id, culture, refId) {
    $('#editorModal').modal('show');
    $('#formctnr').html('');
    $.get(action, { id: id, selectedCulture: culture, refId: refId }, function (result) {
        if (action !== 'SpecificationValues') {
            $('#editorModal .langarea').show();
            $('#editorModal .langarea button:not([data-culture=' + culture + '])').removeClass('btn-primary').addClass('btn-default');
            $('#editorModal .langarea button[data-culture=' + culture + ']').addClass('btn-primary');
        } else {
            $('#editorModal .langarea').hide();
        }

        $('#editorModalLoading').hide();
        $('#formctnr').html(result);
    });
}

function removeModel(action, id) {
    $.ajax(action, {
        data: { id: id },
        type: "POST",
        beforeSend: function () {
            $('#confirmDialogLoading').show();
        },
        success: function (result) {
            switch (result.status) {
                case 200:
                    if (document.confirmDialog.data('param') === 1) {
                        $('#specification-groups tr[data-id=' + id + ']').remove();
                    } else {
                        $('#specification-groups tr[data-specificationid=' + id + ']').remove();
                    }
                    break;
                default:
                    $('#confirmAlert div[data-alertcontent]').html(result.message);
                    $('#confirmAlert').modal('show');
                    break;
            }
        }
    }).always(function () {
        $('#confirmDialog').modal('hide');
        $('#confirmDialogLoading').hide();
    });;
}